/**************************************************************************************************
  Filename:       misc_func.h
  Revised:        $Date: 2014-12-04 10:20:18 +0800 (Fri, 04 Apr 2014) $
  Revision:       $Revision: 10001 $

  Description:    This file contains miscellaneous functions.
**************************************************************************************************/
#ifndef __MISC_FUNCTION_H__
#define __MISC_FUNCTION_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include <stdlib.h>

#include "hal_types.h"
#include "hal_timer.h"
#include "comdef.h"
#include "bcomdef.h"

#include "gap.h"
#include "gatt.h"
#include "hci.h"

#include "gapgattserver.h"
#include "gattservapp.h"
#include "gatt_profile_uuid.h"


#include "peripheral.h"
#include "gapbondmgr.h"
#include "OSAL.h"
#include "OSAL_Clock.h" 

#include "ioCC2541.h"
#include "hal_drivers.h"
#include "hal_mcu.h"
#include "bsp_storage.h"
#include "hal_adc.h"

#include "devinfoservice.h"
#include "battprofile.h" 
#include "dataservice.h"
  

//GAP Peripheral Role desired connection parameters

// Use limited discoverable mode to advertise for 30.72s, and then stop, or
// use general discoverable mode to advertise indefinitely
// #define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_LIMITED
#define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_GENERAL

// Minimum connection interval (units of 1.25ms, 80=100ms) if automatic parameter update request is enabled
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL     6

// Maximum connection interval (units of 1.25ms, 800=1000ms) if automatic parameter update request is enabled
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL     800

// Slave latency to use if automatic parameter update request is enabled
#define DEFAULT_DESIRED_SLAVE_LATENCY         0

// Supervision timeout value (units of 10ms, 1000=10s) if automatic parameter update request is enabled
#define DEFAULT_DESIRED_CONN_TIMEOUT          10000

// Whether to enable automatic parameter update request when a connection is formed
#define DEFAULT_ENABLE_UPDATE_REQUEST         TRUE

// Connection Pause Peripheral time value (in seconds)
#define DEFAULT_CONN_PAUSE_PERIPHERAL         5


// GAP - Advertisement data (max size = 31 bytes, though this is
// best kept short to conserve power while advertisting)
static uint8 advertData[] =
{
  0x02,   // length of first data structure (2 bytes excluding length byte)
  GAP_ADTYPE_FLAGS,   // AD Type = Flags
  DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,

  // service UUID, to notify central devices what services are included
  // in this peripheral
  0x05,   // length of second data structure (7 bytes excluding length byte)
  // GAP_ADTYPE_SERVICES_LIST_16BIT,   // list of 16-bit UUID's available, but not complete list
  GAP_ADTYPE_16BIT_MORE,
  LO_UINT16( DATA_SERV_UUID ),
  HI_UINT16( DATA_SERV_UUID ),
  LO_UINT16( BATT_SERV_UUID ),
  HI_UINT16( BATT_SERV_UUID )
};

// GAP - SCAN RSP data (max size = 31 bytes)
static uint8 deviceName[] =
{
  // complete name  4e 65 78 74 20 47 65 6e 65
  0x05,   // length of first data structure (11 bytes excluding length byte)
  GAP_ADTYPE_LOCAL_NAME_COMPLETE,   // AD Type = Complete local name
  0x42,   // 'B'
  0x65,   // 'e'
  0x62,   // 'b'
  0x65,    // 'e'
  // connection interval range
  0x05,   // length of this data
  GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
  LO_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),   // 100ms
  HI_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),
  LO_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),   // 1s
  HI_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),

  // Tx power level
  0x02,   // length of this data
  GAP_ADTYPE_POWER_LEVEL,
  0       // 0dBm
};


// GAP GATT Attributes
static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = "Bebe";

// ADC voltage levels
// BATT_ADC_LEVEL_2V
// BATT_ADC_LEVEL_3V
// Battery level is critical when it is less than this %
#define DEFAULT_BATT_CRITICAL_LEVEL           6

extern void enMPUInterrupt(void);

extern void GAP_Setup(void);
extern void batteryMonitorInit(void);


#ifdef __cplusplus
}
#endif

#endif
